import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

import router, { asyncRoutes } from '@/router'
import store from './store'
// 添加动态标题
import getPageTitle from '@/utils/get-page-title'

// 路由守卫
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const whiteList = ['/login', '/404']
  console.log('页面跳转之前一定会经过这里')
  console.log('去哪里', to)
  console.log('哪里去', from)
  // 设置动态标题
  document.title = getPageTitle(to.meta.title)
  // 判断
  const token = store.state.user.token
  if (token) {
    console.log('登录放行')
    if (to.path === '/login') {
      console.log('已经登录不用再登录了')
      next('/')
      NProgress.done()
    } else {
      // 获取用户信息
      // await 成功取回数据之后才能进行跳转
      if (!store.state.user.UserInfo.userId) {
        // 需要获取到用户的页面级别的权限信息

        const menus = await store.dispatch('user/getUserInfo')
        console.log(menus, 'menus')
        // 筛选出menus和动态路由筛选出给的权限 让其只能访问有权限的页面
        const filterRoutes = asyncRoutes.filter(item => {
          console.log(item)
          return menus.includes(item.children[0].name)
        })
        // 添加动态路由
        router.addRoutes(filterRoutes)// 白屏状态的原因，因为路由跳转是异步的，还没有加载完成就直接跳转了页面，出现了白屏的状态

        // 解决404的bug 因为路由path: '*'可以匹配所有的路由 当第一个路由会自动匹配*所以单刷新的时候会自动匹配404不会经过路由，将404 移到动态路由的后面
        router.addRoutes([ // 404 page must be placed at the end !!!
          { path: '*', redirect: '/404', hidden: true }])
        store.commit('menys/updateMenus', filterRoutes)
        return next({ ...to, replace: true })// 解决添加动态路由添加出现白屏的状态
      }

      // console.log(store.state.user.UserInfo.userId)
      console.log('已登录,放行')
      next()
    }
  } else {
    console.log('没有token')
    // 查看是否有白名单
    if (whiteList.includes(to.path)) {
      console.log('有白名单，放行')
      next()
    } else {
      console.log('没有白名单去登录页面')
      next('/login')
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
